//
//  GroupSetUpHeaderView.m
//  PigMarket
//
//  Created by mac on 2019/6/18.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "GroupSetUpHeaderView.h"
#import "GroupSetUpHeaderCell.h"

@interface GroupSetUpHeaderView ()<UICollectionViewDelegate,UICollectionViewDataSource>
/**
 collectionView
 */
@property (nonatomic , strong) UICollectionView *collectionView;
/**
 search
 */
@property (nonatomic , strong) UISearchBar *searchView;
/**
 item
 */
@property (nonatomic , strong) NSMutableArray *itemArr;

@end
@implementation GroupSetUpHeaderView
#pragma mark - init method
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initView];
        [self initData];
        [self initLayout];
    }
    return self;
}

- (void)initView {
    // 添加子控件
    [self addSubview:self.collectionView];
}

- (void)initLayout {
    // 布局子控件
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}
- (void)initData {
}
#pragma mark - delegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.dataArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    GroupSetUpHeaderCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GroupSetUpHeaderCellID" forIndexPath:indexPath];
    if (self.dataArr.count >indexPath.item) {
        [cell configCellWithModel:self.dataArr[indexPath.item]];
    }
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    int memberCount = (int)(self.dataArr.count - 2);
    if (indexPath.item == memberCount) {
        //发送通知 ，跳转对应界面
        //➕

        [[NSNotificationCenter defaultCenter] postNotificationName:NotificationAddGroupMemberChange object:self];
        
    } else if (indexPath.item == memberCount + 1) {
        //➖
        [[NSNotificationCenter defaultCenter] postNotificationName:NotificationDeleteGroupMemberChange object:self];

    } else if (indexPath.item < self.dataArr.count){
        
    }
}
#pragma mark - getter and setter
- (void)setDataArr:(NSMutableArray *)dataArr {
    _dataArr = dataArr;
    [self.collectionView reloadData];
}
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.itemSize = CGSizeMake(50, 80);
        flowLayout.minimumLineSpacing = 10;
        flowLayout.minimumInteritemSpacing = 10;
        flowLayout.sectionInset = UIEdgeInsetsMake(10, 15, 20, 10);
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        [_collectionView registerClass:[GroupSetUpHeaderCell class] forCellWithReuseIdentifier:@"GroupSetUpHeaderCellID"];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
    }
    return _collectionView;
}

@end

